﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryManagementSystem
{
    public partial class Addwindow : Form
    {
        public Addwindow()
        {
            InitializeComponent();           
        }

        public Addwindow(int Id, string Title, string Author, string CreatedTime2)
        {
            InitializeComponent();
            this.id = Id;
            textBox4.Text = Id.ToString();
            textBox1.Text = Title;
            textBox2.Text = Author;
            textBox3.Text = CreatedTime2;
        }

        private void button2_Click(object sender, EventArgs e)  //取消更新
        {
            this.Close();
        }

        private int id;
        private void button1_Click(object sender, EventArgs e)  //保存更新
        {
            var id2 = textBox4.Text;
            var Title = textBox1.Text;
            var Author = textBox2.Text;
            var CreatedTime = textBox3.Text;
            var judgeS = !string.IsNullOrEmpty(Author);
            if (id > 0)
            {
                if (judgeS)
                {
                    var sql = string.Format("update Articles set Id='{0}',Title='{1}',Author='{2}',CreatedTime='{3}'", id2, Title, Author, CreatedTime);
                    var result2 = DbHelper.AddorUpdateOrDelete(sql);
                    if (result2 == 1)
                    {
                        MessageBox.Show("更新成功");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("更新失败！数据不能为空！");
                    }
                }

            }else 
            {
                if (judgeS) {
                    var sql = string.Format("insert into Articles(Id,Title,Author,CreatedTime)  values ('{0}','{1}','{2}','{3}')",id2,Title,Author,CreatedTime);
                    var dt = DbHelper.AddorUpdateOrDelete(sql);
                    if (dt == 1)
                    {
                        MessageBox.Show("添加成功！");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败！");
                    }
                }
                
            }
        }
    }
}
